from SystematicFactors.General import *
import os
import Core.Gadget as Gadget
# 货币现象
# ---Currency---
# ---央行公开操作---
# OMO因子
def Calc_OMO_Central_Bank_Operation(database, datetime1, datetime2):
    #
    df = EDB('M0061614', datetime1, datetime2, 'CB_NetInvested')  # 公开市场操作:货币净投放（周）
    df2 = EDB('M0041371', datetime1, datetime2, 'CB_ReverseRepo7')  # 逆回购利率:7天
    df3 = EDB('M0041373', datetime1, datetime2, 'CB_ReverseRepo14')  # 逆回购利率:14天
    df4 = EDB('M0329545', datetime1, datetime2, 'MLF_1Y')  # 中期借贷便利(MLF):利率:1年
    #
    Save_Systematic_Factor_To_Database(database, df2, 'CB_ReverseRepo7')
    Save_Systematic_Factor_To_Database(database, df3, 'CB_ReverseRepo14')
    Save_Systematic_Factor_To_Database(database, df4, 'MLF_1Y')

    # 周数据计算，本来频率就是周度
    df.index = pd.to_datetime(df.index)
    df["Release_Date"] = df.index
    # print(df)
    df_weekly = df.resample("W").agg({"CB_NetInvested": "sum", "Release_Date": "last"})
    df_weekly["Report_Date"] = df_weekly.index
    print(df_weekly)
    Save_Systematic_Factor_To_Database(database, df_weekly, save_name='CB_NetInvested_Weekly',
                                       field_name='CB_NetInvested')

    # 月度修正
    df = Fix_Trim_Monthly(df)
    df_monthly = df.resample("M").agg({"CB_NetInvested": "sum", "Release_Date": "last"})
    df_monthly["Report_Date"] = df_monthly.index
    print(df_monthly)
    #
    Save_Systematic_Factor_To_Database(database, df_monthly, save_name='CB_NetInvested_Monthly',
                                       field_name='CB_NetInvested')


# ---利率引导---
def Calc_OMO_Slf_Mlf_Psl(database, datetime1, datetime2):
    #
    df_slf = Query_Data(database, 'M5462036', datetime1, datetime2)  # 常备借贷便利(SLF)余额
    df_psl = Query_Data(database, 'M5528822', datetime1, datetime2)  # 抵押补充贷款(PSL):期末余额
    df_mlf = Query_Data(database, 'M5515072', datetime1, datetime2)  # 中期借贷便利(MLF):期末余额
    df_balance = pd.concat([df_slf, df_psl, df_mlf], axis=1, sort=True)
    # print(df_balance)
    df_balance['sum'] = df_balance.sum(axis=1)
    df_balance['dif'] = df_balance['sum'].diff()
    df_balance['Report_Date'] = df_balance.index
    Fill_ReleaseDate(df_balance, lag_release_month=1, release_day=6)
    # print(df_balance)
    #
    df_omo = EDB('M0061614', datetime1, datetime2, 'CB_NetInvested')  # 公开市场操作:货币净投放
    df_omo["date"] = df_omo.index

    #
    df_calender = Gadget.Generate_Calender_Days_DataFrame(datetime1, datetime2, date_field_name="date")

    df = pd.merge(df_calender, df_omo, how="left", on="date")
    df = pd.merge(df, df_balance, how="left", left_on="date", right_on="Release_Date")
    df["date_t"] = df["date"]
    df.set_index("date_t", inplace=True)
    df.fillna(0, inplace=True) # 不进行填充，

    # 构建周度数据, 采用月度数据仅在月底向周度数据对齐的方式
    # print(df)
    df_weekly = df[["dif", "CB_NetInvested", "date"]].resample("W").agg({"CB_NetInvested": "sum", "dif": "sum", "date": "last"})
    df_weekly["Report_Date"] = df_weekly.index
    df_weekly["Release_Date"] = df_weekly["date"]
    df_weekly["Total_OMO"] = df_weekly["dif"] + df_weekly["CB_NetInvested"]
    # print(df_weekly)

    # 构建月度数据
    df_monthly = df_omo.resample("M").agg({"CB_NetInvested": "sum"})
    df_monthly["Report_Date"] = df_monthly.index
    df_monthly = pd.merge(df_monthly, df_balance, how="left", on="Report_Date")
    df_monthly["Total_OMO"] = df_monthly["dif"] + df_monthly["CB_NetInvested"]
    # print(df_monthly)
    # df_monthly.to_csv("d:/total_omo.csv")

    #
    Save_Systematic_Factor_To_Database(database, df_balance, save_name='SLF_MLF_PSL', field_name='sum')
    Save_Systematic_Factor_To_Database(database, df_balance, save_name='SLF_MLF_PSL_Monthly_Dif', field_name='dif')
    Save_Systematic_Factor_To_Database(database, df_weekly, save_name='Total_OMO_Weekly', field_name='Total_OMO')
    Save_Systematic_Factor_To_Database(database, df_monthly, save_name='Total_OMO_Monthly', field_name='Total_OMO')


# 货币市场，资金市场
def Calc_Money_Market(database, datetime1, datetime2):
    #
    df1 = EDB('M0041652', datetime1, datetime2, 'Repo001')  # 银行间质押式回购加权利率:1天
    df2 = EDB('M0041653', datetime1, datetime2, 'Repo007')  # 银行间质押式回购加权利率:7天
    #
    df3 = EDB('M0220162', datetime1, datetime2, 'DRepo001')  # 存款类机构质押式回购加权利率:1天
    df4 = EDB('M0220163', datetime1, datetime2, 'DRepo007')  # 存款类机构质押式回购加权利率:7天
    #
    # df = pd.merge(df1, df2, how="left", left_index=True, right_index=True)
    # df = pd.merge(df, df3, how="left", left_index=True, right_index=True)
    # df = pd.merge(df, df4, how="left", left_index=True, right_index=True)
    #
    Save_Systematic_Factor_To_Database(database, df1, 'Repo001')
    Save_Systematic_Factor_To_Database(database, df2, 'Repo007')
    Save_Systematic_Factor_To_Database(database, df3, 'DRepo001')
    Save_Systematic_Factor_To_Database(database, df4, 'DRepo007')

    df_shibor_1m = EDB('M0017141', datetime1, datetime2, 'Shibor_1M')  # SHIBOR:1个月
    df_shibor_3m = EDB('M0017142', datetime1, datetime2, 'Shibor_3M')  # SHIBOR:3个月
    Save_Systematic_Factor_To_Database(database, df_shibor_1m, 'Shibor_1M')
    Save_Systematic_Factor_To_Database(database, df_shibor_3m, 'Shibor_3M')


def Calc_MM_Repo(database, datetime1, datetime2):
    # 使用一月加权平均数据（日数据）
    df40 = Query_Data(database, 'M0017146', datetime1, datetime2)  # 7天回购利率:加权平均:最近1周(B1W)
    df40.index = pd.to_datetime(df40.index)
    #
    df40["Release_Date"] = df40.index
    df_date = df40.resample("M").last()
    df_date = df_date[["Release_Date"]]
    df40 = df40.resample("M").mean()
    df40 = pd.merge(df40, df_date, how="left", left_index=True, right_index=True)
    #
    df40['factor'] = df40['M0017146'].diff()
    df40['Report_Date'] = df40.index
    print(df40)
    df40 = df40[:-1]
    # return(df40,'Repo007_Weighted_Monthly_Dif')
    df40.dropna(subset=["factor"], inplace=True)
    Save_Systematic_Factor_To_Database(database, df40, save_name='Repo007_Weighted_Monthly_Dif', field_name='factor')


# _7reposd
def Calc_MM_Repo_SD(database, datetime1, datetime2):

   df41 = Query_Data(database,'M0041664',datetime1,datetime2)  # 银行间同业拆借加权利率:7天
   df41['factor'] = df41.rolling(22, min_periods=2).std()
   #
   df41.index = pd.to_datetime(df41.index)
   df41["Release_Date"] = df41.index
   df41 = df41.resample("M").last()
   df41['Report_Date'] = df41.index
   print(df41)
   df41 = df41[:-1]
   #return(df41,'Repo007_Weighted_SD')
   Save_Systematic_Factor_To_Database(database, df41, save_name='Repo007_Weighted_SD', field_name='factor')


def Calc_Shibor_Avg(database, datetime1, datetime2):
    #
    data = EDB('SHIBOR3M.IR', datetime1, datetime2, dateAsIndex=True)
    data["Release_Date"] = data.index
    #
    data['SHIBOR3M'] = data['SHIBOR3M.IR'].rolling(window=5, min_periods=1, center=False).mean()
    data_weekly = data.resample('W').last().fillna(method='ffill')
    data_weekly["Shibor_3M_Weekly_Dif"] = data_weekly["SHIBOR3M"].diff(1)
    data_weekly['Report_Date'] = data_weekly.index
    # print(data_weekly)
    Save_Systematic_Factor_To_Database(database, data_weekly, save_name='Shibor_3M_Weekly_Dif')


#
def Calc_FX(database, datetime1, datetime2):
    #
    # datetime1 = datetime.datetime(2020, 7, 9)
    # datetime2 = datetime.datetime(2020, 7, 31)
    #
    df1 = EDB('M0000185', datetime1, datetime2, 'USDCNY_PBOC')  # 中间价:美元兑人民币（央行基准价）
    df2 = EDB('M0067855', datetime1, datetime2, 'USDCNY_Spot_CFETS')  # 即期汇率:美元兑人民币（本币交易系统里的即期价格 China Foreign Exchange Trade System）
    df3 = EDB('M0290205', datetime1, datetime2, 'USDCNH_Spot_OffShore')  # USDCNH:即期汇率（离岸即期（HK）， 一般用CNY表示在岸，CNH表示离岸）
    df4 = EDB('M0068014', datetime1, datetime2, 'USDCNY_NDF1Y_OffShore')  # USDCNY:NDF:1年（离岸远期， 非交割离岸，央妈视其（1Yr）为目标操作）
    df5 = EDB('USDX.FX', datetime1, datetime2, 'USDX')  # 美元指数

    # print(df1)
    # print(Fix_Trim_Monthly(df1))
    # print(Fix_Trim_Weekly(df1))

    #
    Save_Systematic_Factor_To_Database(database, df1, 'USDCNY_PBOC')
    Save_Systematic_Factor_To_Database(database, df2, 'USDCNY_Spot_CFETS')
    Save_Systematic_Factor_To_Database(database, df3, 'USDCNH_Spot_OffShore')
    Save_Systematic_Factor_To_Database(database, df4, 'USDCNY_NDF1Y_OffShore')

    # 离岸-在岸 即期价差
    df_on_off = pd.merge(df2, df3, left_index=True, right_index=True, how="inner")
    df_on_off["USDCNH_USDCNY_Spread"] = df_on_off["USDCNH_Spot_OffShore"] - df_on_off["USDCNY_Spot_CFETS"]

    # 离岸，期限价差
    df_offshore = pd.merge(df3, df4, left_index=True, right_index=True, how="inner")
    df_offshore["USDCNH_Term_Spread"] = df_offshore["USDCNY_NDF1Y_OffShore"] - df_offshore["USDCNH_Spot_OffShore"]

    # 人民币（PBOC）- USDX 价差
    df_rmb2usdx = pd.merge(df1, df5, left_index=True, right_index=True, how="inner")
    df_rmb2usdx['USDCNY_USDX_Spread'] = df_rmb2usdx['USDCNY_PBOC'] - df_rmb2usdx['USDX']

    #
    print(df_on_off)
    print(df_offshore)
    print(df_rmb2usdx)
    #
    Save_Systematic_Factor_To_Database(database, df_on_off, 'USDCNH_USDCNY_Spread')
    Save_Systematic_Factor_To_Database(database, df_offshore, 'USDCNH_Term_Spread')
    Save_Systematic_Factor_To_Database(database, df_rmb2usdx, 'USDCNY_USDX_Spread')

    # 周度、月度收益
    # df1["Release_Date"] = df1.index
    # df1_weekly = df1.resample('W').last()
    # df1_monthly = df1.resample('M').last()


def Calc_FX_Return(database, datetime1, datetime2):
    # 日数据 取月末数 一阶差分
    df_rmb = Query_Data(database, 'M0000185', datetime1, datetime2)  # 央行基准价
    df_usdx = Query_Data(database, 'USDX.FX', datetime1, datetime2)  # 美元指数
    # print(df_rmb)
    #
    df_rmb_weekly = Calc_Period_Return(df_rmb, "M0000185", period="Weekly", is_log=False)
    df_rmb_monthly = Calc_Period_Return(df_rmb, "M0000185", period="Monthly", is_log=True)  # 智投规定
    # print(df_rmb_weekly)
    # print(df_rmb_monthly)
    #
    df_usdx_weekly = Calc_Period_Return(df_usdx, "USDX.FX", period="Weekly", is_log=False)
    df_usdx_monthly = Calc_Period_Return(df_usdx, "USDX.FX", period="Monthly", is_log=False)
    # print(df_usdx_weekly)
    # print(df_usdx_monthly)

    Save_Systematic_Factor_To_Database(database, df_rmb_weekly, save_name='USDCNY_PBOC_Weekly_Return',
                                       field_name='return')
    Save_Systematic_Factor_To_Database(database, df_rmb_monthly, save_name='USDCNY_PBOC_Monthly_Return',
                                       field_name='return')
    Save_Systematic_Factor_To_Database(database, df_usdx_weekly, save_name='USDX_Weekly_Return', field_name='return')
    Save_Systematic_Factor_To_Database(database, df_usdx_monthly, save_name='USDX_Monthly_Return', field_name='return')
    pass


# 外汇储备
def Calc_Fx_Reserve(database, datetime1, datetime2):
    #
    df4 = EDB('M0010049', datetime1, datetime2)  # 官方储备资产:外汇储备
    df4['log'] = np.log(df4['M0010049'])
    df4['factor'] = df4['log'].diff()
    # df4['Report_Date'] = df4.index.astype(str).map(lambda x: pd.datetime.strptime(x, '%Y-%m-%d'))
    df4["Report_Date"] = df4.index
    Fill_ReleaseDate(df4, lag_release_month=1, release_day=9)
    print(df4)
    # return(df4,'FX_Reserve_Dif')
    Save_Systematic_Factor_To_Database(database, df4, save_name='FX_Reserve', field_name='M0010049')
    Save_Systematic_Factor_To_Database(database, df4, save_name='FX_Reserve_Monthly_Dif', field_name='factor')


# 原版中的rd因子
# Rd(HS300)
# Rd(CHNBOND)
def Calc_Deposit_Reserve_Ratio(database, datetime1, datetime2):
    # M0010290 大型存款类机构 变动公告日
    # M0010287 大型存款类机构 变动日
    # M0010289 中小型存款类机构 变动日 （没有公告日数据）
    df = Gadget.Generate_Calender_Days_DataFrame(datetime1, datetime2, date_field_name="Report_Date")

    #
    df_huge = EDB('M0010290', datetime1, datetime2, '大型存款类机构', dateAsIndex=False)  # 人民币存款准备金率:大型存款类金融机构(变动公告日期)
    df_huge["Report_Date"] = pd.to_datetime(df_huge["Report_Date"])
    df_small = EDB('M0010289', datetime1, datetime2, '中小型存款类机构', dateAsIndex=False)  # 人民币存款准备金率:中小型存款类金融机构(变动日期)
    df_small["Report_Date"] = pd.to_datetime(df_small["Report_Date"])
    #
    df = pd.merge(df, df_huge, how="left", on="Report_Date")
    df = pd.merge(df, df_small, how="left", on="Report_Date")

    #
    df.fillna(method="ffill", inplace=True)
    df["Sum"] = df["大型存款类机构"] + df["中小型存款类机构"]
    df["Dif"] = df["Sum"] - df["Sum"].shift(1)
    # print(df)

    # 变频
    df["date_t"] = pd.to_datetime(df["Report_Date"])
    df.set_index("date_t", inplace=True)
    df_weekly = df.resample("W").sum()
    df_date_by_week = df["Report_Date"].resample("W").last()
    df_weekly = pd.merge(df_weekly, df_date_by_week, how="left", left_index=True, right_index=True)
    # print(df_weekly)
    df_weekly["Release_Date"] = df_weekly["Report_Date"]
    df_weekly["Report_Date"] = df_weekly.index

    #
    # 确定正确的起始时间
    df_weekly = df_weekly[(df_weekly["Report_Date"] >= datetime1) & (df_weekly["Report_Date"] <= datetime2)]
    print(df_weekly.tail())

    Save_Systematic_Factor_To_Database(database, df_weekly, save_name="Deposit_Reserve_Ratio_Weekly_Dif",
                                       field_name="Dif")
    pass
    ##
    df6 = Query_Data(database, 'M0061518', datetime1, datetime2)  # 人民币存款准备金率:大型存款类金融机构(月)
    df6['factor'] = df6.diff()
    df6['Report_Date'] = df6.index.astype(str).map(lambda x: pd.datetime.strptime(x, '%Y-%m-%d'))
    Fill_ReleaseDate(df6, lag_release_month=1, release_day=1)
    print(df6)
    Save_Systematic_Factor_To_Database(database, df6, save_name='Deposit_Reserve_Ratio_Monthly_Dif',
                                       field_name='factor')


# 季度数据转换为月度数据，其余补0
def Calc_Excess_Reserve_Ratio(database, datetime1, datetime2):
    #
    datetime1 += datetime.timedelta(days=-180)
    documents = Gadget.generate_end_date_of_month_list(datetime1.date(), datetime2.date(), as_date=True)
    df = pd.DataFrame(documents, columns=["date"])
    df["date"] = pd.to_datetime(df["date"])
    #
    df7 = Query_Data(database, 'M0010096', datetime1, datetime2)  # 超额存款准备金率(超储率):金融机构
    df7["date"] = pd.to_datetime(df7.index)
    df7['Report_Date'] = df7["date"]
    Fill_ReleaseDate(df7, lag_release_month=2, release_day=15)
    # print(df7)

    df = pd.merge(df, df7, how="left", on="date")
    df.fillna(method="ffill", inplace=True)
    # print(df)

    df['factor'] = df["M0010096"].diff()
    df['Report_Date'] = df["date"]
    # 季后第二个月中旬
    # print(df)
    df.dropna(subset=["factor"], inplace=True)
    Save_Systematic_Factor_To_Database(database, df, save_name='Excess_Reserves_Ratio_Monthly_Dif', field_name='factor')
    return(df, 'Excess_Reserves_Ratio_Dif')


#
def Calc_Ted_Spread(database, datetime1, datetime2):
    #
    df1 = Query_Data(database,'M0017142', datetime1, datetime2)  # SHIBOR:3个月
    df2 = Query_Data(database,'S0059741', datetime1, datetime2)  # 中债国债到期收益率:3个月
    df = pd.merge(df1, df2, how="inner", left_index=True, right_index=True)
    # df['yearmonth'] = df.index.map(lambda x: 100*x.year + x.month)
    # df11 = df11.groupby('yearmonth').mean()
    df["Report_Date"] = pd.to_datetime(df.index)
    df['Release_Date'] = df["Report_Date"]
    df.index = pd.to_datetime(df.index)
    df['sub'] = df['M0017142'] - df['S0059741']
    # print(df)
    #
    df_Monthly = df.resample("M").mean()
    df_Monthly2 = df.resample("M").last()
    df_Monthly2 = df_Monthly2[["Release_Date"]]
    df_Monthly = pd.merge(df_Monthly, df_Monthly2, how="left", left_index=True, right_index=True)
    df_Monthly = df_Monthly[:-1]
    # df_Monthly['sub'] = df_Monthly['M0017142'] - df_Monthly['S0059741']
    df_Monthly['factor'] = df_Monthly['sub'].diff()
    df_Monthly['Report_Date'] = df_Monthly.index
    print(df_Monthly)
    #
    Save_Systematic_Factor_To_Database(database, df, save_name='Ted_Spread', field_name='sub')
    Save_Systematic_Factor_To_Database(database, df_Monthly, save_name='Ted_Spread_Avg_Monthly', field_name='sub')
    #
    df_Monthly.dropna(inplace=True)
    Save_Systematic_Factor_To_Database(database, df_Monthly, save_name='Ted_Spread_Avg_Monthly_Dif',
                                       field_name='factor')
    return(df,'Ted_Spread')


def Calc_CHN_US_IR_Spread(database, datetime1, datetime2):
    #
    datetime0 = datetime1 + datetime.timedelta(days=-30)
    #
    df_us = EDB('G0000886', datetime0, datetime2, '美国国债收益率1年', dateAsIndex=False)  # 美国:国债收益率:1年
    df_cn = EDB('S0059744', datetime0, datetime2, '中国国债收益率1年', dateAsIndex=False)  # 中债国债到期收益率:1年
    #
    df = pd.merge(df_us, df_cn, how="inner", on="Report_Date")
    df["Spread"] = df["中国国债收益率1年"] - df["美国国债收益率1年"]
    df["Release_Date"] = df["Report_Date"]
    #
    # print(df)
    #
    df["date_t"] = pd.to_datetime(df["Report_Date"])
    df.set_index("date_t", inplace=True)
    df_weekly = df.resample("W").last()
    df_weekly["Report_Date"] = df_weekly.index

    # 防止空白周，如春节，黄金周
    df_weekly.dropna(subset=["Spread"], inplace=True)
    df_weekly["Dif"] = df_weekly["Spread"] - df_weekly["Spread"].shift(1)
    df_weekly.dropna(subset=["Dif"], inplace=True)
    #
    # print(df_weekly.tail())
    #
    Save_Systematic_Factor_To_Database(database, df_weekly, save_name="CHN_US_IR_Spread_1Y", field_name="Spread")
    Save_Systematic_Factor_To_Database(database, df_weekly, save_name="CHN_US_IR_Spread_1Y_Weekly_Dif",
                                       field_name="Dif")


def Calc_CHN_US_IR_Spread_Month_Avg(database, datetime1, datetime2):

    df1 = Query_Data(database, 'S0059744',datetime1,datetime2)  # 中债国债到期收益率:1年
    df2 = Query_Data(database, 'G0000886',datetime1,datetime2)  # 美国:国债收益率:1年
    df42 = pd.merge(df1, df2, how="inner", left_index=True, right_index=True)

    df42.index = pd.to_datetime(df42.index)
    df42["Release_Date"] = df42.index

    df42 = df42.resample("M").agg({"S0059744":"mean", "G0000886":"mean", "Release_Date":"last"})
    df42['sub'] = df42['S0059744'] - df42['G0000886']
    df42['factor'] = df42['sub'].diff()
    # df42['factor'] = np.log(df42['sub'] / df42['sub'].shift(1))

    df42['Report_Date'] = df42.index
    # print(df42)
    df42 = df42[:-1]

    # return(df42,'dif_chn_us_irs1y')
    df42.dropna(subset=["factor"], inplace=True)
    Save_Systematic_Factor_To_Database(database, df42, save_name='CHN_US_IR_Spread_1Y_Avg_Monthly_Dif',
                                       field_name='factor')


def Calc_Hotmoney(database, datetime1, datetime2):
    #
    df1 = Query_Data(database,'M0010049',datetime1,datetime2)  # 官方储备资产:外汇储备
    df2 = Query_Data(database,'M0009870',datetime1,datetime2)  # 实际使用外资金额:外商直接投资:当月值
    df3 = Query_Data(database,'M5200002',datetime1,datetime2)  # 非金融类对外直接投资:当月值
    df4 = Query_Data(database,'M5207849',datetime1,datetime2)  # 银行结售汇差额:当月值
    df5 = pd.concat([df1,df2,df3,df4],axis=1)
    df5.fillna(0,inplace=True)  # 这个因子全部用0填充了
    #
    df5['diff'] = df5['M0010049'].diff()  # 外储变动
    df5['sub'] = df5['M0009870'] - df5['M5200002']  # 投资差额
    df5['factor'] = df5['diff'] - df5['sub'] - df5['M5207849']
    # df5['Report_Date'] = df5.index.astype(str).map(lambda x: pd.datetime.strptime(x, '%Y-%m-%d'))
    df5['Report_Date'] = df5.index

    # M5207849公布最晚 每月25日公布数据 因子月更新
    Fill_ReleaseDate(df5, lag_release_month=1, release_day=20)
    # print(df5)
    Save_Systematic_Factor_To_Database(database, df5, save_name='Hot_Money_Monthly_Dif', field_name='factor')
    return(df5,'Hot_Money_Dif')


# ---Credit---
# 2022-2-7
def download_swap_rate(database, datetime1, datetime2):
    #
    df_1 = EDB('M1004036', datetime1, datetime2, 'FR_007_Swap_1Y')  # FR007利率互换定盘曲线_均值:1Y
    df_2 = EDB('M1004040', datetime1, datetime2, 'FR_007_SWap_5Y')  # FR007利率互换定盘曲线_均值:5Y
    df_3 = EDB('M1004003', datetime1, datetime2, 'Shibor_3M_Swap_1Y')  # Shibor3M利率互换定盘曲线_均值:1Y
    df_4 = EDB('M1004007', datetime1, datetime2, 'Shibor_3M_Swap_5Y')  # Shibor3M利率互换定盘曲线_均值:5Y
    #
    Save_Systematic_Factor_To_Database(database, df_1, save_name='FR_007_Swap_1Y')
    Save_Systematic_Factor_To_Database(database, df_2, save_name='FR_007_SWap_5Y')
    Save_Systematic_Factor_To_Database(database, df_3, save_name='Shibor_3M_Swap_1Y')
    Save_Systematic_Factor_To_Database(database, df_4, save_name='Shibor_3M_Swap_5Y')



if __name__ == '__main__':
    #
    path_filename = os.getcwd() + "\..\Config\config_local.json"
    database = Config.create_database(database_type="MySQL", config_file=path_filename, config_field="MySQL")

    w.start()

    datetime1 = datetime.datetime(2024, 12, 1)
    datetime2 = datetime.datetime(2025, 1, 19)
    # datetime1 = datetime2 + datetime.timedelta(days=-60)
    #
    # download_swap_rate(database, datetime1, datetime2)
    Calc_OMO_Central_Bank_Operation(database, datetime1, datetime2)
    Calc_OMO_Slf_Mlf_Psl(database, datetime1, datetime2)
    # Calc_Credit_Spread(database, datetime1, datetime2)
    # Calc_Shibor_Avg(database, datetime1, datetime2)
    # Calc_Trust(database, datetime1, datetime2)
    # Calc_FX(database, datetime1, datetime2)
    # Calc_FX_Return(database, datetime1, datetime2)
    # Calc_Fx_Reserve(database, datetime1, datetime2)
    # Calc_Hotmoney(database, datetime1, datetime2)